﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Common;

namespace AI.FSM
{
    /// <summary>
    /// 巡逻
    /// </summary>
    public class PatrollingState : FSMState
    {
        private int index;
        protected override void Init()
        {
            StateID = FSMStateID.Patrolling;
        }
        public override void EnterState(FSMBase fsm)
        {
            base.EnterState(fsm);
            fsm.isPatrolComplete = false;//刚进入状态，巡逻未完成
            fsm.chAnim.SetBool(fsm.chStatus.animParams.Walk, true);
        }
        public override void Action(FSMBase fsm)
        {
            base.Action(fsm);
            //如果到达目标点
            if (Vector3.Distance(fsm.transform.position, fsm.wayPoints[index].position) <= fsm.patrolArrivalDistance)
            {
                //最后一个路点
                if (index == fsm.wayPoints.Length - 1)
                {
                    switch (fsm.patrolMode)
                    {
                        case PatrolMode.Once:
                            fsm.isPatrolComplete = true;//完成巡逻
                            return;
                        case PatrolMode.PingPong:
                            Array.Reverse(fsm.wayPoints);//数组反转
                            break;
                        default:
                            break;
                    }
                }
                //索引增加，对应Loop巡逻
                index = (index + 1) % fsm.wayPoints.Length;//取余好，周期性变化用
            }
            //移动
            fsm.MoveToTarget(fsm.wayPoints[index].position, fsm.walkSpeed, fsm.patrolArrivalDistance);
            //旋转
            fsm.transform.LookPosition(fsm.wayPoints[index].position, fsm.rotateSpeed);
            //switch (fsm.patrolMode)
            //{
            //    case PatrolMode.Once:
            //        OncePatrolling(fsm);
            //        break;
            //    case PatrolMode.Loop:
            //        LoopPatrolling(fsm);
            //        break;
            //    case PatrolMode.PingPong:
            //        PingPongPatrolling(fsm);
            //        break;
            //    default:
            //        break;
            //}
            ////旋转
            //fsm.transform.LookPosition(fsm.wapPoints[index].position, fsm.rotateSpeed);
        }
        public override void ExitState(FSMBase fsm)
        {
            base.ExitState(fsm);
            fsm.chAnim.SetBool(fsm.chStatus.animParams.Walk, false);
            fsm.StopMove();
        }
        //private void OncePatrolling(FSMBase fsm)
        //{
        //    //单次巡逻
        //    //如果到达目标点
        //    if (Vector3.Distance(fsm.transform.position, fsm.wapPoints[index].position) <= fsm.patrolArrivalDistance)
        //    {
        //        //到达的目标点是最后一个路点
        //        if (index == fsm.wapPoints.Length - 1)
        //        {
        //            fsm.isPatrolComplete = true;//完成巡逻
        //            return;
        //        }
        //        index++;
        //    }
        //    //移动
        //    fsm.MoveToTarget(fsm.wapPoints[index].position, fsm.walkSpeed, fsm.patrolArrivalDistance);
        //}
        //private void LoopPatrolling(FSMBase fsm)
        //{
        //    //循环巡逻
        //    //如果到达目标点
        //    if (Vector3.Distance(fsm.transform.position, fsm.wapPoints[index].position) <= fsm.patrolArrivalDistance)
        //    {
        //        //index = (index + 1) <= fsm.wapPoints.Length - 1 ? index + 1 : 0;
        //        index = (index + 1) % fsm.wapPoints.Length;//取余好，周期性变化用
        //    }
        //    //移动
        //    fsm.MoveToTarget(fsm.wapPoints[index].position, fsm.walkSpeed, fsm.patrolArrivalDistance);
        //}
        //private void PingPongPatrolling(FSMBase fsm)
        //{
        //    //乒乓巡逻
        //    //如果到达目标点
        //    if (Vector3.Distance(fsm.transform.position, fsm.wapPoints[index].position) <= fsm.patrolArrivalDistance)
        //    {
        //        //到达的目标点是最后一个路点
        //        if (index == fsm.wapPoints.Length - 1)
        //        {
        //            Array.Reverse(fsm.wapPoints);//数组反转
        //        }
        //        index = (index + 1) % fsm.wapPoints.Length;//取余好，周期性变化用
        //    }
        //    //移动
        //    fsm.MoveToTarget(fsm.wapPoints[index].position, fsm.walkSpeed, fsm.patrolArrivalDistance);
        //}
    }
}

